package com.grape.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grape.domain.CategorySetting;
import com.grape.mapper.CategorySettingMapper;
import com.grape.service.ICategorySettingService;
import com.grape.constants.Constants;
import com.grape.utils.StringUtils;
import com.grape.utils.ThreadLocalUtil;
import com.grape.vo.BaseVo;
import com.grape.wrapper.BaseWrapper;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.List;

/**
 *
 * 分类表 服务实现类
 *
 * @author      putao
 * @email       2628279194@qq.com
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class CategorySettingServiceImpl extends ServiceImpl<CategorySettingMapper, CategorySetting> implements ICategorySettingService {

    private final CategorySettingMapper categorySettingMapper;

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(CategorySettingServiceImpl.class);

    /**
     * 分页
     * @param baseVo    统一分页对象
     * @return          分类表集合
     */
    @Override
    public List<CategorySetting> listData(BaseVo baseVo) {
        QueryWrapper<CategorySetting> wrapper = new BaseWrapper<CategorySetting>().init();
        wrapper.like(Strings.isNotBlank(baseVo.getSearch()), Constants.NAME, baseVo.getSearch())
                .eq(StringUtils.isNotBlank(baseVo.getColumnName()) && StringUtils.isNotBlank(baseVo.getSelect()), baseVo.getColumnName(), baseVo.getSelect());
        orderByModifyTimeDesc(wrapper);
        return this.list(wrapper);
    }

    /**
     * 自定义分页
     * @param   baseVo      统一分页对象
     */
    @Override
    public void pageListCustom(BaseVo baseVo) {
        String userId = ThreadLocalUtil.getUserId();
        QueryWrapper<CategorySetting> qw = new QueryWrapper<CategorySetting>().eq(Constants.USER_ID, userId)
                .orderByDesc(Constants.CREATE_TIME);
        this.categorySettingMapper.selectList(qw);
    }

    /**
     * 修改
     * @param categorySetting 分类表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void updateData(CategorySetting categorySetting) {
        this.checkIsRepeat(w -> w.eq(Constants.TYPE, categorySetting.getType()), categorySetting, "name", MessageFormat.format("分类配置名字：{0} 已存在", categorySetting.getName()));
        this.baseUpdate(categorySetting);
    }

    /**
     * 新增
     * @param       categorySetting 分类表对象
     * @return      分类表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public CategorySetting insertData(CategorySetting categorySetting) {
        categorySetting.setUserId(ThreadLocalUtil.getUserId());
        this.checkIsRepeat(w -> w.eq(Constants.TYPE, categorySetting.getType()), categorySetting, "name", MessageFormat.format("分类配置名字：{0} 已存在", categorySetting.getName()));
        return this.baseInsert(categorySetting);
    }

    /**
     * 删除
     * @param id    单据id
     * @return      删除条数
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int deleteData(String id) {
        return this.baseDelete(id);
    }

    /**
     * 根据id查询详情
     * @param       id  单据id
     * @return      分类表对象
     */
    @Override
    public CategorySetting detail(String id) {
        return this.getByIdForDelFlag(id);
    }

    /**
     * 查询全部
     * @return      分类表集合
     */
    @Override
    public List<CategorySetting> listAll() {
        return categorySettingMapper.selectList(new QueryWrapper<CategorySetting>().eq(Constants.DEL_FLAG, Constants.ZERO_STR));
    }
}
